//
//  StrategyView.m
//  wedding
//
//  Created by 常宽 on 15/1/24.
//  Copyright (c) 2015年 常宽. All rights reserved.
//
/**********攻略***********/
#import "StrategyView.h"
#import "StrategyModel.h"// 本系列的model类

@interface StrategyView ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
/// 数据数组
@property (nonatomic, retain) NSMutableArray *dataArray;
@end

@implementation StrategyView

- (instancetype) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.dataArray = [[NSMutableArray alloc] init];
        
        [self netWorking];
    }
    return self;
}

- (void)netWorking
{
    NSString *urlString = @"http://data.halobear.cn/mapi/index.php?act=articlecate&pid=0";
    [NetHandler networkWithURL:urlString completion:^(id result) {
        NSArray *array = [result objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            StrategyModel *model = [[StrategyModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self createTableView];
}
///  创建视图
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self addSubview:self.tableView];
}

#pragma mark -
#pragma mark tabelView协议
///  section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
///  每个section里的row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
///  返回tableView里的cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"Strategy";
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[BaseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    StrategyModel *model = [self.dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = model.cate_name;
    return cell;
}
/// 返回row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
///  选中cell执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    StrategyModel *model = [self.dataArray objectAtIndex:indexPath.row];
    self.tiaozhuanBlock(model);
}

#pragma mark -

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
